from sqlalchemy import Column, Integer, String, DateTime, Enum as SQLEnum, ForeignKey, Boolean, Text
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
import enum

Base = declarative_base()

class ScoreType(enum.Enum):
    TEACHER = "teacher"
    STUDENT = "student"

class Score(Base):
    __tablename__ = "scores"
    
    id = Column(Integer, primary_key=True, index=True)
    group_id = Column(String, index=True)
    score = Column(Integer)
    type = Column(SQLEnum(ScoreType))
    created_at = Column(DateTime, default=datetime.now)

class Student(Base):
    __tablename__ = "students"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String)
    group_id = Column(String, index=True) 

class ClassStatus(Base):
    __tablename__ = "class_status"
    
    id = Column(Integer, primary_key=True, index=True)
    is_active = Column(Boolean, default=False)
    started_at = Column(DateTime, default=datetime.now) 

class ClassSession(Base):
    __tablename__ = "class_sessions"
    
    id = Column(Integer, primary_key=True, index=True)
    started_at = Column(DateTime, default=datetime.now)
    is_active = Column(Boolean, default=True)

class Question(Base):
    __tablename__ = "questions"
    
    id = Column(Integer, primary_key=True, index=True)
    session_id = Column(Integer, ForeignKey('class_sessions.id'))  # 关联到课程会话
    question_number = Column(Integer)  # 在当前会话中的题号
    content = Column(String)
    option_a = Column(String)
    option_b = Column(String)
    option_c = Column(String)
    option_d = Column(String)
    correct_answer = Column(String, nullable=True)  # 添加正确答案字段
    is_active = Column(Boolean, default=True)
    created_at = Column(DateTime, default=datetime.now)

class StudentAnswer(Base):
    __tablename__ = "student_answers"
    
    id = Column(Integer, primary_key=True, index=True)
    student_id = Column(Integer, ForeignKey('students.id'))
    question_id = Column(Integer, ForeignKey('questions.id'))
    answer = Column(String)
    is_correct = Column(Boolean, nullable=True)  # 修改为允许为空
    submitted_at = Column(DateTime, default=datetime.now) 